package com.gz.controller;

import com.gz.common.CmmentService;
import com.gz.common.model.Evaluate;
import com.gz.utils.Response;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Page;

public class CommentController extends Controller {

	/**
	 * 查询商品的所有评论信息
	 * 
	 * @param productId  商品id
	 * @param pageNumber 当前页
	 * @param pageSize   显示数据
	 * @param pageSize   显示数据
	 * @URL http://localhost:8080/cms-api/comment/findEvaluateByProductPage?productId=4
	 */
	public void findEvaluateByProductPage() {
		Integer pageNumber = getParaToInt("pageNumber");
		Integer pageSize = getParaToInt("pageSize");
		Integer productId = getParaToInt("productId");
		Page<Evaluate> pageEvaluate = CmmentService.getCmmentService().findEvaluateByProductPage(pageNumber, pageSize,
				productId);
		if (pageEvaluate != null && pageEvaluate.getPageSize() > 0) {
			renderJson(Response.responseJson(1006, "成功！", pageEvaluate));
		} else {
			renderJson(Response.responseJson(1007, "失败！", null));
		}
	}

	/**
	 * @author 尹公子 新增评论
	 * @param content
	 * @param starclass
	 * @param order_id
	 * @param userid
	 * @param productid
	 * @return ture、false
	 */
	public void getAddPingLun() {
		Evaluate evaluate = new Evaluate();
		// 评论内容
		String content = getPara("content");
		// 评论星级
		Integer starclass = getParaToInt("starclass");
		// 订单id
		Integer order_id = getParaToInt("order_id");
		// 评论的用户
		Integer userid = getParaToInt("userid");
		// 评论商品的编号
		Integer productid = getParaToInt("productid");
		evaluate.setContent(content);
		evaluate.setStarclass(starclass);
		evaluate.setOrderId(order_id);
		evaluate.setUserid(userid);
		evaluate.setProductid(productid);
		if (CmmentService.getCmmentService().addPingLun(evaluate)) {
			renderJson(Response.responseJson(1004, "发布评论成功"));
		} else {
			renderJson(Response.responseJson(1005, "发布评论失败"));
		}
	}

	/**
	 * 3
	 */

	/**
	 * 4
	 */

	public void getCommentCount() {
		Integer productId = getParaToInt("productId");
		int result = CmmentService.getCmmentService().getCommentCount(productId);
		renderJson(Response.responseJson(1001, "成功！", result));
	}

}
